﻿/**
//  ImageEntity.cs
//  Marquee Moment
//  Copyright 2011 Systematix Technocrates. All rights reserved.
**/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace MarqueeMoment.Entities
{
    /// <summary>
    /// This is the class to keep all the transaction method from tblImage
    /// </summary>
    public class ImageEntity
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        #region Fields

        private Int32 imageId;
        private byte[] imageData;


        private List<ImageEntity> imageList;

        #endregion Fields

        #region Constructors

        public ImageEntity()
            : base()
        {
            imageId = 0;
            imageData = new byte[0];
            imageList = new List<ImageEntity>();
        }


        #endregion Constructors

        #region Properties

        public Int32 ImageId
        {
            get { return imageId; }
            set { imageId = value; }
        }

        public byte[] ImageData
        {
            get { return imageData; }
            set { imageData = value; }
        }



        #endregion Properties

        #region Methods

        #region Public

        #endregion Public

        #region Private

        #endregion Private

        #endregion Methods

        #region Static methods

        #region Public

        /// <summary>
        /// To select all the records from the tblImage table
        /// </summary>
        /// <returns></returns>    
        public static List<ImageEntity> SelectAll()
        {
            List<ImageEntity> imageList = null;
            ImageEntity image = null;
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return null;

            cmd.CommandText = "[spSelectAllImage]";
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);
                if (dataSet.Tables.Count > 0)
                    if (dataSet.Tables[0].Rows.Count > 0)
                    {
                        imageList = new List<ImageEntity>();
                        for (int count = 0; count < dataSet.Tables[0].Rows.Count; count++)
                        {
                            image = new ImageEntity();
                            image.ImageId = Convert.ToInt32(dataSet.Tables[0].Rows[count][tblImage.ImageId]);
                            image.ImageData = (byte[])dataSet.Tables[0].Rows[count][tblImage.ImageData];

                            imageList.Add(image);
                        }
                    }
            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Select All Images");
            }
            finally
            {
                if (dataSet != null)
                    dataSet.Dispose();

                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return imageList;
        }
        /// <summary>
        /// To select all the records from tblImage and return Dataset of the records
        /// </summary>
        /// <returns></returns>
        public static DataSet SelectAllImage()
        {
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return null;

            cmd.CommandText = "[spSelectAllImage]";
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);
                if (dataSet.Tables.Count > 0)
                    return dataSet;

            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Select All Image");
            }
            finally
            {
                if (dataSet != null)
                    dataSet.Dispose();

                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return null;
        }
              
        /// <summary>
        /// To delete the records of the tblImage table
        /// </summary>
        /// <param name="imageId">image Id is the Id to delete the Particular Record /param>
        public static void Delete(int imageId)
        {
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return;

            cmd.CommandText = "[spDeleteImage]";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@" + tblImage.ImageId, imageId);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);
            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Delete Image/Images");
            }
            finally
            {
                if (dataSet != null)
                    dataSet.Dispose();

                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
        }
        /// <summary>
        /// To count the total record of the tblImage
        /// </summary>
        /// <returns></returns>
        public static Int32 SelectRowCountImage()
        {
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return 0;

            cmd.CommandText = "[spSelectRowCountArchive]";
            cmd.CommandType = CommandType.StoredProcedure;
            #endregion Initialization

            try
            {
                return (int)cmd.ExecuteScalar();
            }

            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Select Row Count Images");
            }
            finally
            {
                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return 0;
        }


        /// <summary>
        /// To select the Image from tblImage table
        /// </summary>
        /// <param name="sqlQuery">It contains the sqlquary to select the records from the table</param>
        /// <returns></returns>
        public static DataSet SelectSQLImage(String sqlQuery)
        {
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return null;

            cmd.CommandText = sqlQuery;
            cmd.CommandType = CommandType.Text;

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);
                return dataSet;
            }
            catch (Exception exc)
            {
               
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Select Image");
            }
            finally
            {
                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return null;
        }

        /// <summary>
        /// To insert the records in tblImage
        /// </summary>
        /// <param name="picparameter"> picparameter consists of the bit image to store in db</param>
        public static Int32 Insert(SqlParameter picparameter)
        {
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return 0;

            cmd.CommandText = "[spInsertimage]";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(picparameter);
            #endregion Initialization

            try
            {
                return (int)cmd.ExecuteScalar();
            }
            catch (Exception exc)
            {

                Dialogs.Error("Error at Insert Image");
                logger.Error(exc.StackTrace);
            }
            finally
            {
                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return 0;
        }

        /// <summary>
        /// To select particular record from tblImage and return ImageEntity
        /// </summary>
        /// <returns></returns>
        public static ImageEntity Select(Int32 imageId)
        {
            ImageEntity image = null;
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return null;

            cmd.CommandText = "[spSelectImageById]";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@" + tblImage.ImageId, imageId);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);
                if (dataSet.Tables.Count > 0)
                    if (dataSet.Tables[0].Rows.Count > 0)
                    {
                        image = new ImageEntity();
                        image.ImageId = Convert.ToInt32(dataSet.Tables[0].Rows[0][tblImage.ImageId]);
                        image.ImageData = (byte[])(dataSet.Tables[0].Rows[0][tblImage.ImageData]);
                    }
            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Select ImageEntity");
            }
            finally
            {
                if (dataSet != null)
                    dataSet.Dispose();

                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return image;
        }
        #endregion Public

        #endregion Static methods
    }
}
